package com.indusfo.spc.pda.service.impl;

import com.indusfo.spc.pda.mapper.StationDosingDetailMapper;
import com.indusfo.spc.pda.mapper.StationDosingMapper;
import com.indusfo.spc.pda.pojo.StationDosing;
import com.indusfo.spc.pda.pojo.StationDosingDetail;
import com.indusfo.spc.pda.service.StationDosingDetailService;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @ClassName StationDosingDetailServiceImpl
 * @Description
 * @Author louk
 * @Date 2019/12/26 16:54
 */
@Service
public class StationDosingDetailServiceImpl implements StationDosingDetailService {
    private static final Logger logger= LoggerFactory.getLogger(StationDosingDetailServiceImpl.class);

    @Resource
    StationDosingDetailMapper stationDosingDetailMapper;
    @Resource
    StationDosingMapper stationDosingMapper;
    @Override
    @Transactional
    public JSONObject insertStationDosingDetail(StationDosingDetail stationDosingDetail) {
        String checkParamResult=checkParam(stationDosingDetail);
        if(!StringUtils.isEmpty(checkParamResult)){
            return JSONObject.build(JSONObject.ERROR,checkParamResult);
        }
        StationDosing stationDosing = stationDosingMapper.queryDosingState(stationDosingDetail.getStationDosingId());
        if(stationDosing==null){
            return JSONObject.build(JSONObject.ERROR,"未获取到相关信息");
        }
        if(stationDosing.getDosingState()==2){
            return JSONObject.build(JSONObject.ERROR,"当前叫料已经配送完成无需配送");
        }
       /* Integer noDosingNum = stationDosing.getNoDosingNum();
        if(noDosingNum!=null && !"".equals(noDosingNum) &&  noDosingNum<=0){
            return JSONObject.build(JSONObject.ERROR,"当前叫料已经配送完成无需配送");
        }
        if(noDosingNum!=null && Integer.parseInt(stationDosingDetail.getDosingNum())>stationDosing.getNoDosingNum()){
            return JSONObject.build(JSONObject.ERROR,"配送数量大于当前需求数量");
        }
        if((noDosingNum==null || "".equals(noDosingNum)) && Integer.parseInt(stationDosingDetail.getDosingNum()) >Integer.parseInt(stationDosing.getDemadNum())){
            return JSONObject.build(JSONObject.ERROR,"配送数量大于当前需求数量");
        }*/
        int insert = stationDosingDetailMapper.insert(stationDosingDetail);
        if(insert>0){
            //更新对应的工位叫料
//            stationDosingMapper.updateDosingState(stationDosingDetail.getStationDosingId());
            return JSONObject.oK("配料成功");
        }
        return JSONObject.build(JSONObject.ERROR,"配料失败");
    }

    public String checkParam(StationDosingDetail stationDosingDetail){
        Integer stationDosingId = stationDosingDetail.getStationDosingId();
        if(stationDosingId==null || "".equals(stationDosingId)){
            return "未获取到相关产线叫料信息";
        }
        String dosingNum = stationDosingDetail.getDosingNum();
        if(StringUtils.isEmpty(dosingNum)){
            return "未获取到配送数量";
        }
        if(StringUtils.isNumeric(dosingNum)){
            if(Integer.parseInt(dosingNum)==0 || "".equals(dosingNum)){
                return "配送数量必须为整数并且不能为0";
            }
        } else{
            return "配送数量必须为整数";
        }
        return null;
    }
}
